package com.khaled.protclass.model.bean.weka;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import com.khaled.protclass.model.base.BaseFeature;
import com.khaled.protclass.model.base.IFeature;
import com.khaled.protclass.model.util.Constants;

public class WekaFile
{
	private String relation;

	private ArrayList<Attribute> attributes;

	private String CLASS_ATTRIBUTES = "@attribute class {0,1}";

	private ArrayList<String> dataList;

	public WekaFile(ArrayList<BaseFeature> features, String relationName)
	{
		this.attributes = new ArrayList<Attribute>();

		// 1. Load Weka Attributes
		for (IFeature f : features)
		{
			addAttributes(f.getAttributes());
		}

		// 2. Set relation name for weka file
		setRelation(relationName);

		// 3. Initialize dataList
		dataList = new ArrayList<String>();
	}

	public void addAttribute(Attribute attribute)
	{
		this.attributes.add(attribute);
	}

	public void addAttributes(ArrayList<Attribute> attributesList)
	{
		this.attributes.addAll(attributesList);
	}

	public void addFeatureVector(String featureVector)
	{
		this.dataList.add(featureVector);
	}

	public void addFeatureVectors(ArrayList<String> featureVectors)
	{
		this.dataList.addAll(featureVectors);
	}

	public void setRelation(String relation)
	{
		this.relation = relation;
	}

	public void save(String outputPath) throws IOException
	{
		FileWriter fstream = new FileWriter(outputPath);
		BufferedWriter out = new BufferedWriter(fstream);

		out.write(Constants.WEKA_RELATION + " " + this.relation + "\n");

		for (Attribute att : this.attributes)
		{
			out.write(att.toString() + "\n");
		}

		out.write(CLASS_ATTRIBUTES + "\n");

		out.write(Constants.WEKA_DATA + "\n");

		for (String fv : this.dataList)
		{
			out.write(fv + "\n");
		}

		out.close();
		fstream.close();
	}
}
